package com.bw.mvvm_common.mvvm.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.mvvm.model.BaseModel;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @author KK
 * @description:
 * @date :2021/10/12 15:12
 */
public abstract class BaseViewModel<M extends BaseModel> extends ViewModel implements IViewModel, Observer<BaseEntity> {

    protected M m;
    protected CompositeDisposable compositeDisposable;
    private UIChanage uiChanage;


    @Override
    public void oncreate() {
        //初始化LiveData封装
        compositeDisposable = new CompositeDisposable();
        initModel();
    }

    @Override
    public void onDestory() {
        compositeDisposable.dispose();
        compositeDisposable.clear();
    }

    //反射获取泛型class类型->创建对象
    private void initModel(){
        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
        Type[]types = parameterizedType.getActualTypeArguments();
        Class clazz = (Class) types[0];
        try {
            m = (M) clazz.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    protected abstract void result(BaseEntity baseEntity);

    @Override
    public void onSubscribe(@NotNull Disposable d) {
        compositeDisposable.add(d);
        showDialog();
    }

    @Override
    public void onNext(@NotNull BaseEntity baseEntity) {
        result(baseEntity);
    }

    @Override
    public void onError(@NotNull Throwable e) {
        showMsg("请求出错:"+e.getMessage());
    }

    @Override
    public void onComplete() {
        hideDialog();
    }


    //提供到View层进行LiveData数据绑定
    //数据驱动View层对应观察者
    //观察者下调用对应封装的显示对话框等方法
    public UIChanage getUiChanage() {
        if(uiChanage==null){
            uiChanage = new UIChanage();
        }
        return uiChanage;
    }

    protected void showDialog(){
        getUiChanage().setShowDialogObservable(""+System.currentTimeMillis());
    }

    protected void hideDialog(){
        getUiChanage().setHideDialogObservable(""+System.currentTimeMillis());
    }

    protected void finishPage(){
        getUiChanage().setFinishActObservable(""+System.currentTimeMillis());
    }

    protected void showMsg(String msg){
        getUiChanage().setShowMsgObservble(msg);
    }

    protected void startPage(HashMap<String,Object> map){
        getUiChanage().setStartPageObservable(map);
    }
    public class UIChanage{
        private MutableLiveData<String>showDialogObservable = new MutableLiveData<>();
        private MutableLiveData<String>hideDialogObservable = new MutableLiveData<>();
        private MutableLiveData<String>showMsgObservble = new MutableLiveData<>();
        private MutableLiveData<String>finishActObservable = new MutableLiveData<>();
        private MutableLiveData<HashMap<String,Object>>startPageObservable = new MutableLiveData<>();

        public void setFinishActObservable(String finishActObservable) {
            this.finishActObservable.setValue(finishActObservable);
        }

        public void setHideDialogObservable(String hideDialogObservable) {
            this.hideDialogObservable.setValue(hideDialogObservable);
        }

        public void setShowDialogObservable(String showDialogObservable) {
            this.showDialogObservable.setValue(showDialogObservable);
        }

        public void setShowMsgObservble(String showMsgObservble) {
            this.showMsgObservble.setValue(showMsgObservble);
        }

        public void setStartPageObservable(HashMap<String, Object>startPageObservable) {
            this.startPageObservable.setValue(startPageObservable);
        }

        public MutableLiveData<HashMap<String, Object>> getStartPageObservable() {
            return startPageObservable;
        }

        public MutableLiveData<String> getFinishActObservable() {
            return finishActObservable;
        }

        public MutableLiveData<String> getShowDialogObservable() {
            return showDialogObservable;
        }

        public MutableLiveData<String> getHideDialogObservable() {
            return hideDialogObservable;
        }

        public MutableLiveData<String> getShowMsgObservble() {
            return showMsgObservble;
        }

    }
}
